package com.yd.app.mapper.provider;

import com.yd.domain.searchParam.NoticeParam;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-03-09 14:20
 * @desc 公告
 **/
public class NoticeProvider  extends BaseProvider{
    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/9
     * @Param
     */
    public String searchNoticeList(Map map){
        NoticeParam noticeParam= (NoticeParam) map.get("noticeParam");
        StringBuilder sb=new StringBuilder("select n.id,n.`status`,n.title,n.des,n.publishTime,n.publishUser,s.realName as publishUserName,dic.title as statusName from business_notice n " +
                " LEFT JOIN sys_dic dic on dic.`code`=n.`status` AND dic.type='noticeStatus' " +
                " LEFT JOIN sys_user s on s.id=n.publishUser" +
                " WHERE 1=1 ");
        sb.append(createSearchNotWhereSql(noticeParam));
        sb.append(" order by status desc,publishTime desc ");
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据条数
     * @Date:  2018/3/7
     */
    public String searchNoticeCount(Map map){
        NoticeParam noticeParam= (NoticeParam) map.get("noticeParam");
        StringBuilder sb=new StringBuilder("SELECT COUNT(1) from business_notice n  " +
                "  where 1=1 ");
        sb.append(createSearchNotWhereSql(noticeParam));
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询条件
     * @Date:  2018/3/7
     */
    private String createSearchNotWhereSql(NoticeParam noticeParam){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(noticeParam.getTitle())){
            sb.append(" and n.title like  '%"+noticeParam.getTitle()+"%'  ");
        }
        if(!StringUtils.isEmpty(noticeParam.getStatus())){
            sb.append(" and n.`status` = '"+noticeParam.getStatus()+"'  ");
        }
        if(!StringUtils.isEmpty(noticeParam.getStartTime())){
            sb.append(" and n.publishTime >=#{noticeParam.getStartTime}  ");
        }
        if(!StringUtils.isEmpty(noticeParam.getEndTime())){
            sb.append(" and n.publishTime <=#{noticeParam.getEndTime}  ");
        }
        return sb.toString();
    }
}
